﻿<?php
	include 'token.php'; //取得token
	include 'instance_quantity.php'; //取得主機數量
	$ceilmeter_number=$instance_number*3;
	
//ceilmeter
	$url="http://120.96.183.123:8777/v2/meters/cpu_util?limit=$ceilmeter_number";
	//開啟cURL連線
	$cURL = curl_init();
	curl_setopt($cURL, CURLOPT_URL, $url);			
	curl_setopt($cURL, CURLOPT_CUSTOMREQUEST, "GET");
	curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
		'Content-Type: application/json',
		'Accept: application/json',
		'X-Auth-Token:'.$token
	));				
	$cpu_util_data = curl_exec($cURL);
	curl_close($cURL);		
	$alert=null;
	$notification=null;
	
	for ($i=0;$i<$instance_number;$i++)
	{
		$num[$i]=0;
		$instance_id[$i]=json_decode($cpu_util_data,true)[$i]['resource_id'];
		echo $instance_id[$i]."<br/>";
		for ($j=0;$j<$ceilmeter_number;$j++)
		{
			$res_id=json_decode($cpu_util_data,true)[$j]['resource_id'];
			$res_name=json_decode($cpu_util_data,true)[$j]['resource_metadata']['display_name'];
			if($instance_id[$i]==$res_id)
			{
				echo json_decode($cpu_util_data,true)[$j]['resource_metadata']['display_name']."<br/>";
				echo json_decode($cpu_util_data,true)[$j]['recorded_at']."<br/>";
				echo json_decode($cpu_util_data,true)[$j]['counter_unit']."<br/>";
				echo json_decode($cpu_util_data,true)[$j]['counter_volume']."<br/>";
				$counter_volume=json_decode($cpu_util_data,true)[$j]['counter_volume'];
				if ($counter_volume>=0.01)
				{
					$num[$i]=$num[$i]+1;
					if ($num[$i]==3)
					{
						$notification=$notification."「".json_decode($cpu_util_data,true)[$j]['resource_metadata']['display_name']."」";
					}
				}
			}
		}
		
	}
	
	if ($notification!=null)
	{
		$alert="警告";
		$notification="主機".$notification."的CPU使用率過高";
	}
?>
<html>
<form method="post" name="form" id="ceilmeter" action="http://163.15.192.205/vany/index.php/gcm/sendAndroidMsg">
	<input type="text" name="title" id="title1" value=<?php echo $alert?>>
	<input type="text" name="message" id="message1" value=<?php echo $notification?>>
	<input type="submit" name="refer">
</form>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<script src="../js/jquery-1.7.1.min.js"></script>
</head> 
<script type="text/javascript">
		$(document).ready(function() {
		
			if (document.getElementById("title1").value!=null)
			{
					//form.submit();
					$('#ceilmeter').submit();
			}
		
		});
</script>
</html>